package main;

import java.io.BufferedWriter;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;


/**
 * Reads and writes the location of the games (directory name) from txt file GameLocation.txt
 * @author Chris
 *
 */
public class GamesLocator 
{
	
	private static File myFile = new File("src/resources/GameLocation.txt");
	
	public static String[] loadGames()
	{
		File gamesLocation = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(myFile));
			gamesLocation = new File(reader.readLine());
			reader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gamesLocation.list(new FilenameFilter(){

            @Override
            public boolean accept (File dir, String name)
            {
                if (name.endsWith(".java"))
                {
                    return true;
                }
                return false;
            }});
	}
	
	public static void saveLocation(String location)
	{
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(myFile));
			writer.write(location);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}